<?php
/**
 * Description of ConnexionManager_PDO
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Users\Models;


class UsersManager_PDO extends UsersManager{
    
    //put your code here
    public function getInformations(){
        
    }
    public function getUsers(){
        $sql = 'SELECT c.*
                FROM '.$this->nameTable.' as c
                ORDER BY c.pseudo';
        $data=$this->dao->query($sql);
        
        return $this->fecthAssoc_data($data, $this->name);
    }
    public function verifLogin($param1, $param2){
        $sql = 'SELECT u.* FROM '.$this->nameTable.' as u 
                WHERE u.pseudo = :pseudo 
                AND u.password = :password 
                AND u.is_active = 1';
        
        $login = $this->dao->prepare($sql);
        
        $login->BindValue(':pseudo',(string)$param1);
        $login->BindValue(':password',(string)md5($param2));
        
        $login->execute();
 
        return $this->fecthAssoc_data($login, $this->name);
    }
    
    public function verifInscription($param1, $param2){
        $sql = 'SELECT u.id, u.pseudo, u.email FROM '.$this->nameTable.' as u 
                WHERE u.pseudo = :pseudo 
                OR u.email = :email';
        
        $login = $this->dao->prepare($sql);
        
        $login->BindValue(':pseudo',(string)$param1);
        $login->BindValue(':email',(string)$param2);
        
        $login->execute();
 
        return $this->fecthAssoc_data($login, $this->name);
    }
    
    public function addUser($pseudo,$email,$password,$ipPort,$news,$role)
    {
		$sql='INSERT INTO '.$this->nameTable.' (pseudo, email, password, regdate, news, ip, is_active, role) VALUES (:pseudo, :email, :password, NOW(), :news, :ip, 1, :role)';
        {
			
			$insert = $this->dao->prepare($sql);
			
			$insert->bindValue(':pseudo', (string)$pseudo);
			$insert->bindValue(':email', (string)$email);
			$insert->bindValue(':password', (string)md5($password));
			$insert->bindValue(':news', (int)$news);
			$insert->bindValue(':ip', (string)$ipPort);
            $insert->bindValue(':role', (string)$role);
            
			return $insert->execute();
		
        }
	
    }
    
    public function verifIpPort($ipProt)
    {
         $sql = 'SELECT u.ip FROM '.$this->nameTable.' as u 
                WHERE u.ip = :ip';
        
        $login = $this->dao->prepare($sql);
        
        $login->BindValue(':ip',(string)$ipProt);
        
        $login->execute();
 
        return $this->fecthAssoc_data($login, $this->name);
    }
    
    
    
    public function updateUser($user) {
		//$user = new Users();	
		$edit = $this->dao->prepare('
            UPDATE '.$this->nameTable.' 
            SET email = :email, nom = :nom, prenom = :prenom, adresse = :adresse, ville = :ville, cp = :cp, pays = :pays, portable = :portable, fixe = :fixe
            WHERE id = :id');
		
        
		//$edit->bindParam(':pseudo', $this->pseudo);
		$edit->bindParam(':email', $user->getEmail());
		$edit->bindParam(':nom', $user->getNom());
		$edit->bindParam(':prenom', $user->getPrenom());
		$edit->bindParam(':adresse', $user->getAdresse());
		$edit->bindParam(':ville', $user->getVille());
		$edit->bindParam(':cp', $user->getCp());
		$edit->bindParam(':pays', intval($user->getPays()));
		$edit->bindParam(':portable', $user->getPortable());
		$edit->bindParam(':fixe', $user->getFixe());
		//$edit->bindParam(':credits', $this->credits);
		$edit->bindParam(':id', intval($user->getId()));
		return $edit->execute();
	}
    /**
     * met à jour le credit de l'user après encherissement
     * @param array $param
     * @return type 
     */
    public function updateCreditDecrement(array $param){
        $sql = 'UPDATE '.$this->nameTable.' 
            SET credits =credits-1 
            WHERE id = :id';
        
        $req = $this->dao->prepare($sql);
        $req->bindValue(':id', intval($param['iduser']));
		return $req->execute();
        
    }
    
    public function findUserById($id){
        
        return $this->fecthRow_data($this->findById($this->nameTable, $id), $this->name);
    }
    
    public function deleteUser(array $id){
        
        return $this->delete($this->nameTable, $id);
    }
    
    /**
     * liste des produits gagnés par un user
     * @return type 
     */
    public function getProduitsGagnes($id) {
        
		$sql='SELECT e.*
              FROM c2w_encheres as e
              WHERE e.meneur = :id 
                AND "'.date('Y-m-d H:i:s').'" >= e.dateFin 
                AND e.begin ="1"';
        
		$req = $this->dao->prepare($sql);
		$req->bindValue(':id', intval($id));
		$req->execute();
		$out = array();
		foreach( $req->fetchAll(\PDO::FETCH_ASSOC) as $result ) {
			$out[] = $result;
		}
        return $out;
		
    }
    
    public function getEncheresEnCours($id) {
		
        $sql='SELECT e.id, p.nom
              FROM c2w_encherisseurs AS e
              INNER JOIN c2w_encheres AS p ON e.product_id = p.id
              WHERE e.user_id = :id 
                AND "'.date('Y-m-d H:i:s').'" <= p.dateFin 
                AND p.begin ="1"
              GROUP BY e.product_id';
        
		$req = $this->dao->prepare($sql);
		$req->bindValue(':id', intval($id));
		$req->execute();
		$out = array();
		foreach( $req->fetchAll(\PDO::FETCH_ASSOC) as $result ) {
			$out[] = $result;
		}
        return $out;
    }
    
    public function getHistoriqueCredits($id) {
		
		$sql = 'SELECT credits, date
                FROM c2w_credits
                WHERE user_id = :id
                ORDER BY id DESC';
									 
		$req = $this->dao->prepare($sql);
		$req->bindValue(':id', intval($id));
		$req->execute();
		$out = array();
		foreach( $req->fetchAll(\PDO::FETCH_ASSOC) as $result ) {
			$out[] = $result;
		}
        return $out;
		
    }
    
}

?>
